package switchviz.recording;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import switchviz.core.ResourceManager;
import switchviz.util.FileOperations;

public class RecordDialog extends JDialog implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -380548838303894554L;
	private static Logger logger = Logger.getLogger(RecordDialog.class);
	private RecordCreationPanel _recordpanel = null;
	private JButton _buttaply = null;
	private JButton _buttcancel = null;
	private static File _file = null;
	private static String[] _aliaslist = null;
	
	public RecordDialog(Frame owner)
	{
		super(owner, true);
		//super(owner, Dialog.ModalityType.DOCUMENT_MODAL);
		_file = null;
		_aliaslist = null;
		try
		{
			initialize();
			this.setSize(500, 300);
			this.setLocationRelativeTo(null);
			this.setTitle("Configure the Record");
			//this.setVisible(true);
		}
		catch(Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static String[] getAliasList()
	{
		return _aliaslist;
	}
	
	public void setAliasList(String[] aliaslist)
	{
		_recordpanel.setSelection(aliaslist);
	}
	
	public static File getRecordFile()
	{
		if(_file != null)
			return _file;
		else
			return null;
	}
	
	public RecordCreationPanel getRecordPanel() throws Exception
	{
		if(_recordpanel == null)
		{
			_recordpanel = new RecordCreationPanel();
			_recordpanel.setBorder(BorderFactory.createLoweredBevelBorder());
		}
		return _recordpanel;
	}
	
	private void initialize() throws Exception
	{
		JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
        buttonpanel.add(Box.createHorizontalGlue());
        
		_buttaply = new JButton("Ok");
		_buttaply.addActionListener(this);
		_buttaply.setActionCommand(ResourceManager.getInstance().getActionApply());
		_buttcancel = new JButton("Cancel");
		_buttcancel.addActionListener(this);
		
		buttonpanel.add(_buttaply);
		buttonpanel.add(_buttcancel);
		
		this.setLayout(ResourceManager.getInstance().getBorderLayout());
		this.add(getRecordPanel(), BorderLayout.CENTER);
		this.add(buttonpanel, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		String command = evt.getActionCommand();
		try
		{
			if(ResourceManager.getInstance().getActionApply().equals(command))
			{
				String recname = getRecordPanel().getRecordName();
				if(recname == null || recname.length() < 1)
				{
					JOptionPane.showMessageDialog(this,
	    				    "Please insert a Name for the Record.",
	    				    "Form not complete!",
	    				    JOptionPane.WARNING_MESSAGE);
	    			return;
				}
				_file = FileOperations.getUniqueDir(ResourceManager.getInstance().getRecordFolder(), recname);
				_aliaslist = getRecordPanel().getSelectedAlias();
				this.setVisible(false);
			}
			else
			{
				this.setVisible(false);
			}
		}
		catch(Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
}
